package com.example.myanimator.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Region;
import android.graphics.RegionIterator;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import androidx.annotation.Nullable;

import com.example.myanimator.R;

/**
 * @author tgw
 * @date 2021/3/16
 * @describe
 */
class DrawRegionPath extends View {

    private Paint mPaint;
    private Path mPath;
    private Canvas mCanvas;
    private String tipStr = "床前明月光";
    private RectF tipBounds = new  RectF(50, 500, 200, 100);
    private RegionIterator regionIterator;

    public DrawRegionPath(Context context) {
        super(context);
    }

    public DrawRegionPath(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        mPaint = new Paint();
        mPaint.setAntiAlias(true); // 去除锯齿
        mPaint.setStrokeWidth(5);//设置笔画宽度
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setTextAlign(Paint.Align.CENTER);//设置文字对齐方式
        mPaint.setColor(Color.WHITE);//设置文字对齐方式
        mPath = new Path();
    }

    @Override
    protected void onDraw(Canvas canvas) {

        RectF rect = new  RectF(50, 500, 200, 100);
        Region region = new Region();
        mPath.addOval(rect, Path.Direction.CW);
        region.setPath(mPath,new Region(100,100,500,500));
//

        initRegion(canvas, region);

        Rect rect2 = new  Rect(100, 100, 400, 200);
        Rect rect3 = new  Rect(200, 0, 300, 300);
        canvas.drawRect(rect2,mPaint);
        canvas.drawRect(rect3,mPaint);

        mPaint.setColor(Color.GREEN);
        mPaint.setStyle(Paint.Style.FILL);
        Region region2 = new Region(rect2);
        Region region3 = new Region(rect3);
        region2.op(region2,region3, Region.Op.INTERSECT);
        Log.d("tgwregion是否为空：",region2.isEmpty()+"--"+region2.isComplex());
        initRegion(canvas,region2);
    }

    private void initRegion(Canvas canvas, Region region) {
        RegionIterator regionIterator = new RegionIterator(region);
        Rect rect1 = new  Rect();
        while (regionIterator.next(rect1)){
            canvas.drawRect(rect1,mPaint);
        }
    }
}
